package pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.implementation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.INodesManager;

public class NodesManager <IdObject> implements INodesManager<IdObject> {

	private Map<IdObject, Integer> nodeMap;
	
	public NodesManager() {
		this.nodeMap = new HashMap<IdObject, Integer>();
	}
	
	public Integer getNode(IdObject id) {
		return this.nodeMap.get(id);
	}
	
	public void setNode(IdObject id, int nodeId) {
		this.nodeMap.put(id, nodeId);
	}
	
	public void serialize(String fileName) throws IOException {
		File f = new File(fileName);
		f.createNewFile();
		OutputStream stream = new FileOutputStream(f);
		ObjectOutput serialStream = new ObjectOutputStream(stream);
		serialStream.writeObject(this.nodeMap);
		this.nodeMap = new HashMap<IdObject, Integer>();
	}
	
	public void deserialize(String fileName) throws IOException {
		File f = new File(fileName);
		InputStream stream = new FileInputStream(f);
		ObjectInput serialStream = new ObjectInputStream(stream);
		try {
			this.nodeMap = (Map<IdObject, Integer>) serialStream.readObject();
		} catch (ClassNotFoundException e) {
			throw new IOException(e.getMessage());
		}
		f.delete();
	}	
}
